<template>
  <!-- 页面主体区域 -->
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/图片1.png" alt />
        <span class="title">管理信息系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <el-menu
          :default-active="nowPath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
        >
          <el-submenu :index="index+''" v-for="(item, index) in menuList" :key="item.id">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                :index="item2.path"
                v-for="item2 in item.children"
                :key="item2.id"
                @click="savePath(item2.path)"
              >{{item2.authName}}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主界面区域 -->
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    <el-footer>Footer</el-footer>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconList: [
        'el-icon-user-solid',
        'el-icon-lock',
        'el-icon-shopping-bag-1',
        'el-icon-notebook-2',
        'el-icon-pie-chart'
      ],
      nowPath: ''
    }
  },
  methods: {
    // 退出登陆
    logout() {
      window.localStorage.clear()
      this.$router.push('/')
    },
    // 获取菜单列表
    getMenuList() {
      this.$http.get('menus').then(res => {
        if (res.status != 200) {
          this.$message.closeAll()
          this.$message('获取数据失败')
        }
        res.data.data.forEach((item, index) => {
          item.icon = this.iconList[index]
          this.menuList.push(item)
        })
      })
      // console.log(this.menuList)
    },
    // 为了高亮目前的菜单项
    savePath(path) {
      this.nowPath = path
      // console.log(this.nowPath)
    }
  },
  mounted() {
    // this.$message.success('welcome')
    this.getMenuList()
  }
}
</script>

<style lang="less">
.el-container {
  height: 100%;
}
.el-header {
  background-color: #343434;
  display: flex;
  justify-content: space-between;
  padding: 10px 10px !important;
  div {
    // 使用flex布局的嵌套实现纵向上的居中对齐
    display: flex;
    align-items: center;
    font-size: 20px;
    font-weight: 700;
  }
  img {
    height: 100%;
    border-radius: 50%;
    margin-right: 20px;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #fff;
}
.el-footer {
  background: yellow;
}
</style>